package com.ruipengkj.commons.util.DBDump;

import com.ruidom.Com.Transform.Util.DataMeta;
import com.ruidom.Com.Transform.Util.Updater;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 12-5-14
 * Time: 上午3:05
 * To change this template use File | Settings | File Templates.
 */
public class XMLObjDataLoader {
    private Connection conn;
    private String table;
    private DataMeta meta = null;
    private String keyCol;
    List<Updater.Match> matches = null;
    DataMeta tometa;
    private boolean updateOnExists;
    PreparedStatement insertStmt = null;

    public XMLObjDataLoader(Connection conn, String table, DataMeta meta, String _keyCol, boolean updateOnExists) {
        this.conn = conn;
        this.table = table;
        keyCol = _keyCol;
        this.tometa = meta;
        this.updateOnExists = updateOnExists;
    }

    public void load(XmlPullParser _xpp, List<String> _colsBuffer) throws XmlPullParserException, IOException, SQLException {
            int type = _xpp.getEventType();
            String curTag = null;
            while(!(type == XmlPullParser.END_TAG && _xpp.getName().equalsIgnoreCase("table")))
            {
                switch(type)
                {
                    case XmlPullParser.START_TAG:
                        curTag = _xpp.getName();
                        if(curTag.equalsIgnoreCase("meta"))
                        {
                            meta = Updater.parseMeta(_xpp);
                        }
                        else if(curTag.equalsIgnoreCase("row"))
                        {
                        }
                        break;
                    case XmlPullParser.TEXT:
                        if(curTag != null && curTag.equalsIgnoreCase("row"))
                        {
                            Updater.setInsertValue(getInsertStmt(), Updater.parse(_xpp.getText(), _colsBuffer), tometa, matches);
                        }
                        break;
                }
                _xpp.next();
                type = _xpp.getEventType();
            }
            if(insertStmt != null)
            {
                insertStmt.executeBatch();
                insertStmt.close();
            }
        }

    public PreparedStatement getInsertStmt() throws SQLException
    {
        if(insertStmt == null)
        {
            matches = Updater.match(tometa, meta);
            if(updateOnExists)
            {
                insertStmt = conn.prepareStatement(Updater.createInsertUpdatePreparedString(table, tometa, matches));
            }
            else
            {
                insertStmt = conn.prepareStatement(Updater.createInsertPreparedString(table, tometa, matches));
            }
        }

        return insertStmt;
    }
}
